﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BAL.EstRec.FDVGrid;

namespace BAL.EstRec.FDVIndicador
{
    public class ModelCapFdvIndicador
    {
        private List<Recurso> recursos;
        public CapFDVIndicadorCollection entry;
        private FDVRecursoRule rule;

        public ModelCapFdvIndicador(List<string> pos)
        {
            this.entry = new CapFDVIndicadorCollection(pos,rule);
        }

        public ModelCapFdvIndicador(CapturasPFDV cpfdv,FDVRecursoRule rule)
        {
            this.rule = rule;

            //Obtener los distintos pos y ordenarlos
            var res2 = (from o in cpfdv.ObCollPFDV
                        orderby o.pos.idPos
                        select
                         o.pos.idPos).Distinct().ToList();

            this.entry = new CapFDVIndicadorCollection();

            
            foreach (string p in res2)
            {
                //CapFDVIndicador cfi = this.Calculate(p, cpfdv);
                //this.entry.Add(cfi);
                this.entry.Add(new CapFDVIndicador(new Pos(p,p),0,0,0,0,new FDVRecursoRule(0,0)));
            }   
        }

        public ModelCapFdvIndicador(CapFdvPivot2Collection pivotFdv, FDVRecursoRule rule,List<Recurso> recursos)
        {
            this.rule = rule;
            this.recursos = recursos;

            this.entry = new CapFDVIndicadorCollection();

            //Se asume que la collection pivotFdv ya está ordenada por pos ascendentemente
            //Iterar atravez de los pos
            foreach (RecursoIndexer p in pivotFdv)
            {
                //CapFDVIndicador cfi = this.Calculate(p);
                this.entry.Add(p.indicador);
            }   
        }

        public CapFDVIndicador Calculate(RecursoIndexer mPivotFdv)
        {
            CapFDVIndicador ind = null;
            //Visitas sugeridas
            var v = 0;
            var listRec=mPivotFdv.ToList();
            try
            {
                v = (from o in listRec
                     join p in this.recursos on o.Key.ToString() equals p.idRecurso.ToString()
                     where o.Value>0
                     group new { o, p } by new { p.idRecurso } into g
                     select new { Recurso = g.Key, maxVal = g.Max(c => c.p.visitasSugeridas) }).Max(m => m.maxVal);
            }
            catch (Exception ex)
            {
                v = 0;
            }

            // tiempo implementación
            var ti=0;
            try
            {
                ti = (from o in listRec
                          join p in this.recursos on o.Key.ToString() equals p.idRecurso.ToString()
                          group new { o, p } by new { p.idRecurso } into g
                          select new { Recurso = g.Key, sumVal = g.Sum(c => c.p.tiempoImplementacion * c.o.Value) }).Sum(m => m.sumVal);
            }
            catch (Exception)
            {

                ti = 0;
            }


            //N{umero de Inicitivas
            var ni = 0;
            try
            {
                ni = (from o in listRec
                      join p in this.recursos on o.Key.ToString() equals p.idRecurso.ToString()
                      where o.Value > 0 && !p.excReglaIniciativa
                      group new { o, p } by new { p.idIniciativa } into g
                      select new { Iniciativa = g.Key }).ToList().Count;
            }
            catch (Exception)
            {

                ni = 0;
            }

            //N{umero de Recusos
            var nr = 0;
            try
            {
                nr = (from o in listRec
                      join p in this.recursos on o.Key.ToString() equals p.idRecurso.ToString()
                      where o.Value > 0 && !p.excReglaMaxRecursos
                      group new { o, p } by new { p.idRecurso } into g
                      select new { Recurso = g.Key }).ToList().Count;
            }
            catch (Exception)
            {

                nr = 0;
            }

            ind = new CapFDVIndicador(mPivotFdv.Pos, ti, v, ni, nr,this.rule);

            return ind;
        }

        public CapFDVIndicador Calculate(string idPos, CapturasPFDV oCPFDV)
        {
            // iniciativas
            int cnt = 0;
            string inic = "";

            foreach (VMCapturaPivotFDV item in oCPFDV.ObCollPFDV)
            {
                if (item.recurso.excReglaIniciativa)
                    cnt++;
            }

            if (cnt > 2)
                inic = "Excedidas";
            else inic = "OK";

            // Visitas sugeridas
            var v = 0;
            try
            {
                v = (from o in oCPFDV.ObCollPFDV
                        where o.pos.idPos == idPos && o.cantidad > 0
                        group o by o.recurso into g
                        select new { Recurso = g.Key, maxVal = g.Max(c => c.recurso.visitasSugeridas) }).Max(m=>m.maxVal);
            }
            catch (Exception ex)
            {
                v = 0;
            }

            // tiempo implementación
            var x = (from o in oCPFDV.ObCollPFDV
                     where o.pos.idPos == idPos
                     group o by o.pos into g
                     select new { Pos = g.Key, sumVal = g.Sum(s => s.recurso.tiempoImplementacion * s.cantidad) }
                    ).Sum(s => s.sumVal);

            //numero de iniciativas
            var numIniciativas = (from o in oCPFDV.ObCollPFDV
                                  where o.pos.idPos == idPos && o.recurso.excReglaIniciativa == false && o.cantidad>0
                                  select o.recurso.idIniciativa).Distinct().ToList().Count;

            //Numero de recursos
            var numRecursos = (from o in oCPFDV.ObCollPFDV
                               where o.pos.idPos == idPos && o.recurso.excReglaMaxRecursos == false && o.cantidad>0
                               select o.recurso.idRecurso).Distinct().ToList().Count;

            return new CapFDVIndicador(new Pos(idPos, idPos), x, v, numIniciativas,numRecursos,rule);

        }//


        public CapFDVIndicador Calculate2(string idPos, CapturasPFDV oCPFDV)
        {
            // iniciativas
            int cnt = 0;
            string inic = "";

            foreach (VMCapturaPivotFDV item in oCPFDV.ObCollPFDV)
            {
                if (item.recurso.excReglaIniciativa)
                    cnt++;
            }

            if (cnt > 2)
                inic = "Excedidas";
            else inic = "OK";

            // Visitas sugeridas
            var v = 0;
            try
            {
                v = (from o in oCPFDV.ObCollPFDV
                     where o.pos.idPos == idPos && o.cantidad > 0
                     group o by o.recurso into g
                     select new { Recurso = g.Key, maxVal = g.Max(c => c.recurso.visitasSugeridas) }).Max(m => m.maxVal);
            }
            catch (Exception ex)
            {
                v = 0;
            }

            // tiempo implementación
            var x = (from o in oCPFDV.ObCollPFDV
                     where o.pos.idPos == idPos
                     group o by o.pos into g
                     select new { Pos = g.Key, sumVal = g.Sum(s => s.recurso.tiempoImplementacion * s.cantidad) }
                    ).Sum(s => s.sumVal);

            //numero de iniciativas
            var numIniciativas = (from o in oCPFDV.ObCollPFDV
                                  where o.pos.idPos == idPos && o.recurso.excReglaIniciativa == false && o.cantidad > 0
                                  select o.recurso.idIniciativa).Distinct().ToList().Count;

            //Numero de recursos
            var numRecursos = (from o in oCPFDV.ObCollPFDV
                               where o.pos.idPos == idPos && o.recurso.excReglaMaxRecursos == false && o.cantidad > 0
                               select o.recurso.idRecurso).Distinct().ToList().Count;

            return new CapFDVIndicador(new Pos(idPos, idPos), x, v, numIniciativas, numRecursos, rule);

        }//

        
    }
}
